home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / pwd / pwdread.c < prev    next >
C/C++ Source or Header  |  1992-03-19  |  3KB  |  117 lines

  1. /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <errno.h>
  21. #include <limits.h>
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <pwd.h>
  27. #include <sys/types.h>
  28.  
  29.  
  30. /* This is the function that all the others are based on.
  31.    The format of the password file is known only here.  */
  32.  
  33. /* Structure containing info kept by each __pwdread caller.  */
  34. typedef struct
  35.   {
  36.     char *buf;
  37.     size_t buflen;
  38.     struct passwd p;
  39.   } pwdread_info;
  40.  
  41.  
  42. /* Return a chunk of memory containing a pre-initialized `pwdread_info'.  */
  43. PTR
  44. DEFUN_VOID(__pwdalloc)
  45. {
  46.   pwdread_info *info = (PTR) malloc (sizeof(pwdread_info));
  47.   if (info == NULL)
  48.     return NULL;
  49.   info->buf = NULL;
  50.   info->buflen = 0;
  51.   return info;
  52. }
  53.  
  54. /* Read a password entry from STREAM, filling in P.  */
  55. struct passwd *
  56. DEFUN(__pwdread, (stream, p), FILE *stream AND PTR CONST p)
  57. {
  58.   register pwdread_info *CONST info = (pwdread_info *) p;
  59.   char *start, *end;
  60.  
  61.   /* Idiocy checks.  */
  62.   if (stream == NULL)
  63.     {
  64.       errno = EINVAL;
  65.       return NULL;
  66.     }
  67.  
  68.   do
  69.     if (__getline (&info->buf, &info->buflen, stream) == -1)
  70.       return NULL;
  71.   while (info->buf[0] == '#');
  72.  
  73.   start = info->buf;
  74.   end = strchr (start, ':');
  75.   if (end == NULL)
  76.     return NULL;
  77.   *end = '\0';
  78.   info->p.pw_name = start;
  79.  
  80.   start = end + 1;
  81.   end = strchr (start, ':');
  82.   if (end == NULL)
  83.     return NULL;
  84.   *end = '\0';
  85.   info->p.pw_passwd = start;
  86.  
  87.   info->p.pw_uid = (uid_t) strtol (end + 1, &end, 10);
  88.   if (*end != ':')
  89.     return NULL;
  90.   info->p.pw_gid = (gid_t) strtol (end + 1, &end, 10);
  91.   if (*end != ':')
  92.     return NULL;
  93.  
  94.   start = end + 1;
  95.   end = strchr (start, ':');
  96.   if (end == NULL)
  97.     return NULL;
  98.   *end = '\0';
  99.   info->p.pw_gecos = start;
  100.  
  101.   start = end + 1;
  102.   end = strchr (start, ':');
  103.   if (end == NULL)
  104.     return NULL;
  105.   *end = '\0';
  106.   info->p.pw_dir = start;
  107.  
  108.   start = end + 1;
  109.   end = strchr (start, '\n');
  110.   if (end == NULL)
  111.     return NULL;
  112.   *end = '\0';
  113.   info->p.pw_shell = start;
  114.  
  115.   return &info->p;
  116. }
  117.